package engine.utils
{
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;

	public class ClassUtils
	{
		/**
		 * Constructs a new Class with the given <code>type</code> and <code>array</code> of parameters.
		 * The number of parameters cannot exceed 10.
		 * 
		 * @param Type: The Class to be instantiated.
		 * @param Array: And array of parameters for the constructor.
		 * @return The instantiated Class with the given parameters.
		 */
		public static function construct(type:Class, array:Array = null):*
		{
			if(!array) return new type();
			
			switch(array.length)
			{
				case 0:
					return new type();
				case 1:
					return new type(array[0]);
				case 2:
					return new type(array[0], array[1]);
				case 3:
					return new type(array[0], array[1], array[2]);
				case 4:
					return new type(array[0], array[1], array[2], array[3]);
				case 5:
					return new type(array[0], array[1], array[2], array[3], array[4]);
				case 6:
					return new type(array[0], array[1], array[2], array[3], array[4], array[5]);
				case 7:
					return new type(array[0], array[1], array[2], array[3], array[4], array[5], array[6]);
				case 8:
					return new type(array[0], array[1], array[2], array[3], array[4], array[5], array[6], array[7]);
				case 9:
					return new type(array[0], array[1], array[2], array[3], array[4], array[5], array[6], array[7], array[8]);
				case 10:
					return new type(array[0], array[1], array[2], array[3], array[4], array[5], array[6], array[7], array[8], array[9]);
				default:
					throw new Error("ClassUtils.construct() :: Parameters cannot exceed a limit of 10.");
					break;
			}
			return type;
		}
		
		/**
		 * Gets the Class of an object instance.
		 * 
		 * @param Object: An Object instance.
		 * @return The instance's Class type.
		 */
		public static function toClass(object:Object):Class
		{
			return getDefinitionByName(getQualifiedClassName(object)) as Class;
		}
	}
}